<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdjustsController extends Controller
{
	//审批首页
    public function getIndex(Request $request){
        if($request['name']){
            //搜索分页
            $old = DB::table('person')
            ->join('adjust','person.id','=','adjust.Personid')
            ->join('department','department.id','=','adjust.OldDepartmentid')
            ->where('PersonName','like','%'.$request['name'].'%')
            ->select('adjust.id','department.DepartmentName','adjust.Status','person.PersonName')
            ->orderby('adjust.Status','asc')
            ->paginate(4);
            $new = DB::table('adjust')
            ->join('department','adjust.NewDepartmentid','=','department.id')
            ->select('adjust.id','department.DepartmentName','adjust.Status')
            ->orderby('adjust.Status','asc')
            ->paginate(4); 
        }else{
            //所有分页
            

            $old = DB::table('person')
            ->join('adjust','person.id','=','adjust.Personid')
            ->join('department','department.id','=','adjust.OldDepartmentid')
            ->select('adjust.id','department.DepartmentName','adjust.Status','person.PersonName')
            ->orderby('adjust.Status','asc')
            ->paginate(4);
            $new = DB::table('adjust')
            ->join('department','adjust.NewDepartmentid','=','department.id')
            ->select('department.DepartmentName','adjust.Status')
            ->orderby('adjust.Status','asc')
            ->paginate(4);
            
            // dd($old);

        }
        // dd($res);
        $info = $request -> all();
        // dd($info);
        $num1 = count(DB::table('adjust')->where('Status',0)->get());
        $num2 = count(DB::table('leaveoffice')->where('Status',0)->get());
        $num3 = count(DB::table('leave')->where('State',0)->get());
        //解析页面
        return view('/allow/adjusts/adjusts',['old'=>$old,'new'=>$new,'info'=>$info,'num1'=>$num1,'num2'=>$num2,'num3'=>$num3]);

    } 
    //审批
    public function getEdit($id){
        //修改审批状态
        DB::table('adjust')->where('id',$id)->update(['Status'=>1]);
        //查询信息
        $info = DB::table('adjust')->where('id',$id)->get();
        // dd($info);
        //修改个人信息中的部门
        DB::table('person')->where('id',$info[0]['Personid'])->update(['DepartmentID'=>$info[0]['NewDepartmentid']]);
        return redirect('allow/adjusts');
    }
    
    
}
